package com.framework;

import java.util.EnumSet;

// 日志装饰器
class LoggingDecorator<T, R> extends ComponentDecorator<T, R> {
    private static final int WARN_THRESHOLD = 200;

    public LoggingDecorator(Component<T, R> delegate, EnumSet<ComponentFeature> features) {
        super(delegate, features);
        features.add(ComponentFeature.LOGGING);
    }

    @Override
    public String getType() {
        return delegate.getType() + "#LOG";
    }

    @Override
    public ComponentOutput<R> execute(ComponentContext<T> context) {
        long start = System.currentTimeMillis();
        System.out.printf("🚀 [%s] 执行开始 | 请求: %s%n",
                delegate.getType(), context.getRequestData().toString());

        try {
            ComponentOutput<R> output = delegate.execute(context);
            long duration = System.currentTimeMillis() - start;

            if (duration > WARN_THRESHOLD) {
                System.out.printf("⚠️ [%s] 执行完成 | 耗时: %dms (超过阈值)%n",
                        delegate.getType(), duration);
            } else {
                System.out.printf("✅ [%s] 执行成功 | 耗时: %dms%n",
                        delegate.getType(), duration);
            }

            return output;
        } catch (Exception e) {
            System.out.printf("❌ [%s] 执行失败 | 错误: %s%n",
                    delegate.getType(), e.getMessage());
            throw e;
        }
    }
}
